package com.life.smile.controller.menu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.life.smile.entity.RearMenu;
import com.life.smile.entity.Role;
import com.life.smile.entity.vo.RearMenuVo;
import com.life.smile.entity.vo.UserLoginLogVo;
import com.life.smile.service.MenuService;
import com.life.smile.utils.ListJsonMessage;
import com.life.smile.utils.Page;
import com.life.smile.utils.ResponseUtil;
/**
 * 菜单权限管理
 */
@Controller
@RequestMapping("jsp/menu")
public class MenuControler {

	 @Autowired
	 private MenuService menuService;
	 
	 //定义拍序列
	 private static String[] columns={"loginTime"}; 
	 /**
	  * 展示所有菜单
	  * @param mv
	  * @return
	  */
	 @RequestMapping(value="/getAllMenu",method=RequestMethod.GET)
	 public ModelAndView getAllMenu(ModelAndView mv){
//		 List<RearMenuVo> list=null;
//		 try {
//			list = menuService.getAllMenu();
//			 mv.addObject("menuList", list);
//			 mv.setViewName("system/menu/menuList");
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//		}
//		mv.addObject("menuList",list);
		mv.setViewName("system/menu/menuList");
		 return mv;
	 }
	 //异步加载菜单数据
	 @ResponseBody
	 @RequestMapping(value = "/selectAllMenuList", method = RequestMethod.GET)
	 
	 public ListJsonMessage selectAllMenuList(String start_date,String end_date,HttpServletRequest req){
		 System.out.println("开始时间："+start_date+"结束时间："+end_date);
		//创建分页参数对象
			Page page = new Page(req,columns);
			Map<String,Object> pmap = page.getPageMap();//获取 分页 排序的 参数
			//除开公共参数外的额外参数
			if(start_date != null && !start_date.equals("")){
				pmap.put("start_date", start_date);
			}	
			if(end_date != null && !end_date.equals("")){
				pmap.put("end_date", end_date);
			}
			//分页数据
			List<RearMenuVo> list = menuService.selectAllMenuList(pmap);
//			for (OrderCreateVo orderCreate : list) {
//				if(orderCreate.getDepartTime()!=null){
//					orderCreate.setDepartTimeString(DateUtils.formatDateTime(orderCreate.getDepartTime()));
//				}if(orderCreate.getOrderTime()!=null){
//					orderCreate.setOrderTimeString(DateUtils.formatDateTime(orderCreate.getOrderTime()));
//				}
//			}
			//分页数据数量
			int count = menuService.getMenuCount(pmap);
			return new ListJsonMessage(page.getDraw(), count, count, list, page.getStart());
	 }
	 
	 /**
	  * 添加菜单页面
	  * @param mv
	  * @return
	  */
	 @RequestMapping(value="/add",method=RequestMethod.GET)
	 public ModelAndView add(ModelAndView mv){
		 mv.setViewName("system/menu/addMenu");
		 return mv;
	 }
	 
	 /**
	  * 查询所有的1.2级目录，用于添加菜单页面的表单回填
	  * @return
	  */
	 @ResponseBody
	 @RequestMapping(value="/getParentMenu",method=RequestMethod.GET)
	 public List<RearMenu> getParentMenu(){
		 List<RearMenu> list = null;
		 try {
			  list = menuService.getParentMenu();
		} catch (Exception e) {
			// TODO: handle exception
		}
		 return list;
	 }
	 
	 
	 /**
	  * 添加菜单
	  * @param m
	  */
	 @RequestMapping(value="/createMenu",method=RequestMethod.POST)
	 public void createMenu(RearMenu m,HttpServletResponse response ){
		 try {
			 menuService.createMenu(m);
			 ResponseUtil.print(response, "success");
		} catch (Exception e) {
			// TODO: handle exception
		}
		 
	 }
	 
	 /**
	  * 删除菜单
	  * @param mid
	  */
	 @RequestMapping(value="/menuDelete",method=RequestMethod.POST)
	 public void menuDelete(Integer mid,HttpServletResponse response){
		 try {
			 System.out.println(mid);
			 menuService.menuDelete(mid);
			 ResponseUtil.print(response, "success");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	 }
	 
	 
	 
	 
	 /**
	  * 查询菜单地址是否已经使用 过
	  * @param url
	  * @param response
	  */
	 @ResponseBody
	 @RequestMapping(value="/checkMenu",method=RequestMethod.POST)
	 public void checkMenu(String url,HttpServletResponse response){
		 try {
			 String flag = menuService.checkMenu(url);
			 if(flag.equals("success")){
				 ResponseUtil.print(response, "success");
			 }
		} catch (Exception e) {
			// TODO: handle exception
		}
		 
	 }
	 
	 /**
	  * 菜单修改页面
	  * @param mv
	  * @return
	  */
	 @RequestMapping(value="/menuUpdata",method=RequestMethod.GET)
	 public ModelAndView menuUpdata(ModelAndView mv,Integer mid){
		 try {
			 RearMenuVo menu = menuService.getMenu(mid);
			 mv.addObject("m", menu);
			 mv.setViewName("system/menu/updateMenu");
		} catch (Exception e) {
			// TODO: handle exception
		}
		 return mv;
	 }
	 /**
	  * 修改菜单
	  * @param mv
	  * @param mid
	  * @return
	  */
	 @RequestMapping(value="/valueUpdata",method=RequestMethod.POST)
	 public void valueUpdata(ModelAndView mv,RearMenu m,HttpServletResponse response){
		 try {
			 menuService.valueUpdata(m);
			 ResponseUtil.print(response, "success");
		} catch (Exception e) {
			// TODO: handle exception
		}
		 
	 }
	 
	 /**
	  * 获取角色列表
	  */
	 @ResponseBody
	 @RequestMapping(value="/getRoleList",method=RequestMethod.GET)
	 public ModelAndView getRoleList(ModelAndView mv){
		 try {
			 List<Role> list = menuService.getRoleList();
			 mv.addObject("roleList", list);
			 mv.setViewName("system/role/roleList");
		} catch (Exception e) {
			// TODO: handle exception
		}
		 return mv;
	 }
	 
	 
	 
	 
	 /**
	  * 添加菜单页面
	  * @param mv
	  * @return
	  */
	 @RequestMapping(value="/roleMenu",method=RequestMethod.GET)
	 public ModelAndView roleMenu(ModelAndView mv,Integer rid){
		 mv.addObject("rid", rid);
		 mv.setViewName("system/role/menu");
		 return mv;
	 }
	 
	 /**
	 * 显示菜单ztree展示
	 * @param id 角色id
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/show_menu",method=RequestMethod.POST)
	@ResponseBody
	public List<RearMenu> show_menu(Integer rid){
		System.out.println(rid);
		List r_list = new ArrayList();
		try{
			List<RearMenu> menu_list = menuService.searchMenuAll(); //查询出 所有的启用菜单
			List<RearMenu> menu_role_list = menuService.searchMenuByRoleId(rid);//通过角色id查询角色拥有菜单
			Map r_map = null;
			for(int j=0;j<menu_list.size();j++){
				Map menu_map = (Map) JSON.toJSON(menu_list.get(j));
				 r_map = new HashMap();
				 r_map.put("id", menu_map.get("mid").toString());
				 r_map.put("pId", menu_map.get("parentid").toString());
				 r_map.put("name", menu_map.get("mname").toString());
				 r_map.put("open", true);
				 for(int i=0;i<menu_role_list.size();i++){
					 Map menu_role_map = (Map)JSON.toJSON(menu_role_list.get(i));
					 if( menu_role_map.get("mid").toString().equals(menu_map.get("mid").toString())){//该角色 拥有该菜单权限
						 r_map.put("checked", true);
					 }
				 }
			 r_list.add(r_map);
		 }
		}catch(Exception e){
			//
		}
		return r_list;
	}
	 
	/**
	 * 修改角色菜单
	 * @param menu_id_str 角色菜单拼接的字符串
	 * @param model
	 * @return
	 */
	 @ResponseBody
	 @RequestMapping(value="/updateRoleMenu",method=RequestMethod.POST)
	 public Model updateRoleMenu(String menu_id_str,Model model,Integer rid){
		 System.out.println(rid+"++++++++++++++"+menu_id_str);
		 String msg = "";
		 try {
			menuService.updateRoleMenu(menu_id_str,rid);
		} catch (Exception e) {
			msg = e.getMessage();
		}
		 if(msg == "" ){
			 return model.addAttribute("msg","修改成功");
		 }
		 return model.addAttribute("msg","系统异常");
	 }
	 
	 
	 /**
	  * 添加菜单页面跳转
	  * @param mv
	  * @return
	  */
	 @RequestMapping(value="/addRole",method=RequestMethod.GET)
	 public ModelAndView addRole(ModelAndView mv){
		 mv.setViewName("system/role/addRole");
		 return mv;
	 }
	 
	 
	 /**
	  * 检查角色是否存在
	  * @param rname
	  * @param response
	  */
	 @ResponseBody
	 @RequestMapping(value="/checkRole",method=RequestMethod.POST)
	 public void checkRole(String rname,HttpServletResponse response){
		 try {
			 boolean flag = menuService.checkRole(rname);
			 if(flag){
				 ResponseUtil.print(response, "success");
			 }
		} catch (Exception e) {
		}
	 }
	 
	 /**
	  * 添加角色
	  * @param m
	  */
	 @RequestMapping(value="/createRole",method=RequestMethod.POST)
	 public void createRole(Role role,HttpServletResponse response ){
		 try {
			 menuService.createRole(role);
			 ResponseUtil.print(response, "success");
		} catch (Exception e) {
		}
	 }
	 
	 /**
	  * 删除角色
	  * @param mid
	  */
	 @RequestMapping(value="/roleDelete",method=RequestMethod.POST)
	 public void roleDelete(Integer rid,HttpServletResponse response){
		 try {
			 menuService.roleDelete(rid);
			 ResponseUtil.print(response, "success");
		} catch (Exception e) {
		}
		
	 }
	 
	 /**
	  * 后台登录日志页
	  */
	 @ResponseBody
	 @RequestMapping(value="/loginLogList",method=RequestMethod.GET)
	 public ModelAndView loginLogList(ModelAndView mv){
		 mv.setViewName("system/loginLog/loginLogList");
		 return mv;
	 }
	 
	 /**
		 * 查询
		 * @param start_date
		 * @param end_date
		 * @param phone
		 * @param req
		 */
	@ResponseBody
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public ListJsonMessage selectAll(String start_date,String end_date,String phone,HttpServletRequest req,String userName){
		//创建分页参数对象
		Page page = new Page(req, columns);
		Map<String,Object> pmap = page.getPageMap();//获取 分页 排序的 参数
		//除开公共参数外的额外参数
		if(userName != null && !userName.equals("")){pmap.put("userName", userName);}
		if(start_date != null && !start_date.equals("")){pmap.put("start_date", start_date);}	
		if(end_date != null && !end_date.equals("")){pmap.put("end_date", end_date);}
		//分页数据
		List<UserLoginLogVo> list = menuService.loginLogList(pmap);
		//分页数据数量
		int count = menuService.getCount(pmap);
		return new ListJsonMessage(page.getDraw(), count, count, list, page.getStart());
	}
	 
	 
	 
	 /**
	  * 获取所有角色
	  */
	 @ResponseBody
	 @RequestMapping(value="/getRoleName",method=RequestMethod.GET)
	 public List<Role> getRoleName(){
		 List<Role> list = null;
		 try {
			 list = menuService.getRoleName();
		} catch (Exception e) {
			
		}
		 return list;
	 }
	 
	 
}
